<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>配置</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f5f5;
            color: #333;
            padding: 20px;
        }

        .container {
            max-width: 100%;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 16px 20px;
            text-align: center;
        }

        .header h2 {
            font-size: 18px;
            font-weight: bold;
        }

        .content {
            padding: 20px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #555;
        }

        .form-input {
            width: 100%;
            padding: 10px 12px;
            border: 2px solid #e1e1e1;
            border-radius: 6px;
            font-size: 14px;
            transition: border-color 0.3s;
        }

        .form-input:focus {
            outline: none;
            border-color: #667eea;
        }

        .form-select {
            width: 100%;
            padding: 10px 12px;
            border: 2px solid #e1e1e1;
            border-radius: 6px;
            font-size: 14px;
            background: white;
            cursor: pointer;
        }

        .form-select:focus {
            outline: none;
            border-color: #667eea;
        }

        .radio-group {
            display: flex;
            flex-direction: column;
            gap: 12px;
            margin-top: 8px;
        }

        .radio-item {
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
        }

        .radio-item input[type="radio"] {
            width: 16px;
            height: 16px;
            cursor: pointer;
        }

        .radio-item label {
            cursor: pointer;
            font-size: 14px;
        }

        .help-text {
            font-size: 12px;
            color: #666;
            margin-top: 4px;
        }

        .buttons {
            display: flex;
            gap: 12px;
            margin-top: 30px;
        }

        .btn {
            flex: 1;
            padding: 12px 20px;
            border: none;
            border-radius: 6px;
            font-size: 14px;
            font-weight: bold;
            cursor: pointer;
            transition: background 0.3s;
        }

        .btn-primary {
            background: #667eea;
            color: white;
        }

        .btn-primary:hover {
            background: #5a6fd8;
        }

        .btn-secondary {
            background: #f1f1f1;
            color: #333;
        }

        .btn-secondary:hover {
            background: #e1e1e1;
        }

        .btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        .status {
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 20px;
            font-size: 14px;
            display: none;
        }

        .status.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .status.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .test-section {
            background: #f8f9fa;
            border-radius: 6px;
            padding: 16px;
            margin-top: 20px;
        }

        .test-title {
            font-weight: bold;
            margin-bottom: 12px;
            color: #495057;
        }

        .test-buttons {
            display: flex;
            gap: 8px;
        }

        .test-btn {
            padding: 8px 16px;
            border: 1px solid #dee2e6;
            background: white;
            border-radius: 4px;
            cursor: pointer;
            font-size: 12px;
            transition: all 0.3s;
        }

        .test-btn:hover {
            background: #e9ecef;
        }

        .test-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        .login-section {
            margin-top: 24px;
            padding: 18px 18px 10px 18px;
            background: #f8f9fa;
            border-radius: 8px;
            border: 1px solid #e1e1e1;
        }
        .login-title {
            font-size: 15px;
            font-weight: bold;
            color: #667eea;
            margin-bottom: 16px;
            text-align: left;
            letter-spacing: 1px;
        }
        .login-row {
            display: flex;
            align-items: center;
            margin-bottom: 14px;
        }
        .login-label {
            width: 80px;
            min-width: 80px;
            text-align: right;
            margin-right: 12px;
            color: #555;
            font-size: 14px;
            letter-spacing: 0;
        }
        .login-input-wrapper {
            position: relative;
            flex: 1;
            display: flex;
            align-items: center;
        }
        .login-input {
            width: 100%;
            padding: 8px 36px 8px 12px;
            border: 1.5px solid #d1d5db;
            border-radius: 5px;
            font-size: 14px;
            background: #fff;
            transition: border-color 0.3s;
            box-sizing: border-box;
        }
        .login-input:focus {
            border-color: #667eea;
            outline: none;
        }
        .toggle-password {
            position: absolute;
            right: 32px;
            cursor: pointer;
            font-size: 18px;
            color: #888;
            user-select: none;
            transition: color 0.2s;
        }
        .toggle-password.active {
            color: #667eea;
        }
        .clear-input {
            position: absolute;
            right: 8px;
            cursor: pointer;
            font-size: 16px;
            color: #bbb;
            user-select: none;
            transition: color 0.2s;
        }
        .clear-input:hover {
            color: #ff4757;
        }
        .login-input[type="password"].fake-mask {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            letter-spacing: 2px;
        }
        .save-notification {
            position: fixed;
            top: 30px;
            left: 50%;
            transform: translateX(-50%);
            background: #2ed573;
            color: #fff;
            padding: 12px 32px;
            border-radius: 8px;
            font-size: 15px;
            font-weight: bold;
            z-index: 9999;
            box-shadow: 0 2px 8px rgba(0,0,0,0.12);
            opacity: 0;
            pointer-events: none;
            transition: opacity 0.3s;
        }
        .save-notification.show {
            opacity: 1;
            pointer-events: auto;
        }
    </style>
</head>
<body>
    <div id="save-notification" class="save-notification"></div>
    <div class="container">
        <div class="header">
            <h2>应用配置</h2>
        </div>

        <div class="content">
            <div class="status" id="status"></div>

            <form id="configForm">
                <div class="form-group">
                    <label class="form-label">连接类型</label>
                    <div class="radio-group">
                        <div class="radio-item">
                            <input type="radio" id="both" name="connectionType" value="both">
                            <label for="both">同时监听（WebSocket + API）</label>
                        </div>
                        <div class="radio-item">
                            <input type="radio" id="api" name="connectionType" value="api" checked>
                            <label for="api">仅API接口</label>
                        </div>
                        <div class="radio-item">
                            <input type="radio" id="ws" name="connectionType" value="websocket">
                            <label for="ws">仅WebSocket</label>
                        </div>
                    </div>
                </div>
                <div class="form-group" id="api-group">
                    <label class="form-label">API接口地址</label>
                    <input type="text" class="form-input" id="apiUrl" placeholder="http://localhost:30001/api/push-data">
                    <div class="help-text">HTTP API接口地址，应用会监听此接口的POST请求</div>
                </div>
                <div class="form-group" id="ws-group">
                    <label class="form-label">WebSocket地址</label>
                    <input type="text" class="form-input" id="wsUrl" placeholder="ws://localhost:8081">
                    <div class="help-text">WebSocket服务器地址，例如：ws://localhost:8081</div>
                </div>

                <div class="form-group" id="polling-group" style="display: none;">
                    <label class="form-label">轮询间隔（毫秒）</label>
                    <input type="number" class="form-input" id="pollingInterval" placeholder="5000" min="1000" max="60000">
                    <div class="help-text">API轮询的时间间隔，建议1000-60000毫秒</div>
                </div>

                <div class="form-group">
                    <label class="form-label">目标网站</label>
                    <input type="text" class="form-input" id="targetUrl" placeholder="https://www.baidu.com">
                    <div class="help-text">接收到数据后要打开的网站地址</div>
                </div>

                <!-- 登录信息区域美化 -->
                <div class="form-group login-section">
                    <div class="login-title">登录信息</div>
                    <div class="login-row">
                        <label class="login-label" for="login-email">电子邮箱:</label>
                        <div class="login-input-wrapper">
                            <input class="login-input" id="login-email" type="email" placeholder="自动登录邮箱" autocomplete="username" />
                            <span class="clear-input" id="clear-email" title="清除">&#10006;</span>
                        </div>
                    </div>
                    <div class="login-row">
                        <label class="login-label" for="login-password">密码:</label>
                        <div class="login-input-wrapper">
                            <input class="login-input" id="login-password" type="password" placeholder="自动登录密码" autocomplete="current-password" />
                            <span class="toggle-password" id="toggle-password" title="显示/隐藏密码">&#128065;</span>
                            <span class="clear-input" id="clear-password" title="清除">&#10006;</span>
                        </div>
                    </div>
                </div>

                <div class="test-section">
                    <div class="test-title">测试配置</div>
                    <div class="test-buttons">
                        <button type="button" class="test-btn" onclick="testConnection()">测试连接</button>
                        <button type="button" class="test-btn" onclick="testTargetUrl()">测试目标网站</button>
                        <button type="button" class="test-btn" onclick="sendTestData()">发送测试数据</button>
                    </div>
                </div>

                <div class="buttons">
                    <button type="button" class="btn btn-secondary" onclick="cancelConfig()">取消</button>
                    <button type="submit" class="btn btn-primary">保存配置</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        const { ipcRenderer } = require('electron');

        let currentConfig = {};
        let realPassword = '';
        let showReal = false;
        const pwdInput = document.getElementById('login-password');
        const toggleBtn = document.getElementById('toggle-password');
        const emailInput = document.getElementById('login-email');
        const clearEmailBtn = document.getElementById('clear-email');
        const clearPwdBtn = document.getElementById('clear-password');

        // 初始化
        document.addEventListener('DOMContentLoaded', () => {
            loadConfig();
            setupEventListeners();
        });

        // 加载配置
        function loadConfig() {
            ipcRenderer.send('get-config');
        }

        // 加载配置时赋值
        function loadConfigToForm(config) {
            // 根据配置设置单选按钮
            if (config.useWebSocket && config.useApi) {
                document.getElementById('both').checked = true;
            } else if (config.useWebSocket && !config.useApi) {
                document.getElementById('ws').checked = true;
            } else if (!config.useWebSocket && config.useApi) {
                document.getElementById('api').checked = true;
            } else {
                // 默认选择API接口
                document.getElementById('api').checked = true;
            }
            
            // 填充表单
            document.getElementById('apiUrl').value = config.apiUrl || '';
            document.getElementById('wsUrl').value = config.wsUrl || '';
            document.getElementById('targetUrl').value = config.targetUrl || '';
            document.getElementById('pollingInterval').value = config.pollingInterval || 5000;
            document.getElementById('login-email').value = config.loginEmail || '';
            realPassword = config.loginPassword || '';
            showReal = false;
            pwdInput.type = 'password';
            if (realPassword.length < 6 && realPassword.length > 0) {
                pwdInput.classList.add('fake-mask');
                pwdInput.value = '******';
            } else {
                pwdInput.classList.remove('fake-mask');
                pwdInput.value = realPassword;
            }
            updateEyeIcon();
            
            toggleConnectionType();
        }

        // 设置事件监听
        function setupEventListeners() {
            // 连接类型切换
            document.querySelectorAll('input[name="connectionType"]').forEach(radio => {
                radio.addEventListener('change', toggleConnectionType);
            });

            // 表单提交
            document.getElementById('configForm').addEventListener('submit', function(e) { saveConfig(e); });

            // 清除按钮逻辑
            clearEmailBtn.addEventListener('click', function() {
                emailInput.value = '';
                emailInput.focus();
            });
            clearPwdBtn.addEventListener('click', function() {
                realPassword = '';
                pwdInput.value = '';
                pwdInput.type = showReal ? 'text' : 'password';
                pwdInput.classList.remove('fake-mask');
                pwdInput.focus();
            });

            // 眼睛图标切换
            function updateEyeIcon() {
                if (showReal) {
                    toggleBtn.classList.add('active');
                    toggleBtn.innerHTML = '&#128064;'; // 实心眼睛
                } else {
                    toggleBtn.classList.remove('active');
                    toggleBtn.innerHTML = '&#128065;'; // 空心眼睛
                }
            }
            toggleBtn.addEventListener('click', function() {
                showReal = !showReal;
                if (showReal) {
                    pwdInput.type = 'text';
                    pwdInput.classList.remove('fake-mask');
                    pwdInput.value = realPassword;
                } else {
                    pwdInput.type = 'password';
                    if (realPassword.length < 6 && realPassword.length > 0) {
                        pwdInput.classList.add('fake-mask');
                        pwdInput.value = '******';
                    } else {
                        pwdInput.value = realPassword;
                    }
                }
                updateEyeIcon();
            });

            pwdInput.addEventListener('input', function() {
                if (showReal) {
                    realPassword = pwdInput.value;
                } else {
                    // 用户在星号状态下输入，忽略
                    if (pwdInput.value !== '******') {
                        realPassword = pwdInput.value;
                    }
                }
                if (!showReal) {
                    if (realPassword.length < 6 && realPassword.length > 0) {
                        pwdInput.classList.add('fake-mask');
                        pwdInput.value = '******';
                    } else {
                        pwdInput.classList.remove('fake-mask');
                    }
                }
            });

            pwdInput.addEventListener('focus', function() {
                if (!showReal && realPassword.length < 6 && realPassword.length > 0) {
                    setTimeout(() => {
                        pwdInput.setSelectionRange(6, 6);
                    }, 0);
                }
            });
        }

        // 切换连接类型
        function toggleConnectionType() {
            const connectionType = document.querySelector('input[name="connectionType"]:checked').value;
            
            if (connectionType === 'both') {
                // 同时监听
                document.getElementById('ws-group').style.display = 'block';
                document.getElementById('api-group').style.display = 'block';
            } else if (connectionType === 'websocket') {
                // 仅WebSocket
                document.getElementById('ws-group').style.display = 'block';
                document.getElementById('api-group').style.display = 'none';
            } else if (connectionType === 'api') {
                // 仅API
                document.getElementById('ws-group').style.display = 'none';
                document.getElementById('api-group').style.display = 'block';
            }
            
            document.getElementById('polling-group').style.display = 'none'; // 轮询间隔不相关
        }

        // 显示状态消息
        function showStatus(message, type = 'success') {
            const status = document.getElementById('status');
            status.textContent = message;
            status.className = `status ${type}`;
            status.style.display = 'block';
            
            setTimeout(() => {
                status.style.display = 'none';
            }, 3000);
        }

        // 保存配置
        function saveConfig(e) {
            if (e) e.preventDefault(); // 阻止表单默认提交
            const connectionType = document.querySelector('input[name="connectionType"]:checked').value;
            
            const newConfig = {
                apiUrl: document.getElementById('apiUrl').value,
                wsUrl: document.getElementById('wsUrl').value,
                targetUrl: document.getElementById('targetUrl').value,
                pollingInterval: parseInt(document.getElementById('pollingInterval').value) || 5000
            };
            
            // 根据选择的连接类型设置配置
            if (connectionType === 'both') {
                newConfig.useWebSocket = true;
                newConfig.useApi = true;
            } else if (connectionType === 'websocket') {
                newConfig.useWebSocket = true;
                newConfig.useApi = false;
            } else if (connectionType === 'api') {
                newConfig.useWebSocket = false;
                newConfig.useApi = true;
            }

            // 保存电子邮箱和密码
            const loginEmail = document.getElementById('login-email').value.trim();
            // 密码保存真实值
            newConfig.loginEmail = loginEmail;
            newConfig.loginPassword = realPassword;
            
            ipcRenderer.send('save-config', newConfig);
        }

        // 取消配置
        function cancelConfig() {
            window.close();
        }

        // 测试连接
        function testConnection() {
            const isWebSocket = document.getElementById('ws').checked;
            const url = isWebSocket ? document.getElementById('wsUrl').value : document.getElementById('apiUrl').value;
            
            if (!url) {
                showSaveNotification('请先输入连接地址', 2500);
                return;
            }

            showSaveNotification('正在测试连接...', 1200);
            
            // 这里可以添加实际的连接测试逻辑
            setTimeout(() => {
                showSaveNotification('连接测试完成（模拟）', 2000);
            }, 1000);
        }

        // 测试目标网站
        function testTargetUrl() {
            const url = document.getElementById('targetUrl').value;
            
            if (!url) {
                showSaveNotification('请先输入目标网站地址', 2500);
                return;
            }

            require('electron').shell.openExternal(url);
            showSaveNotification('已打开目标网站', 2000);
        }

        // 发送测试数据
        function sendTestData() {
            const testData = '测试数据_' + new Date().getTime();
            ipcRenderer.send('test-data', testData);
            showSaveNotification('已发送测试数据', 2000);
        }

        // IPC事件监听
        ipcRenderer.on('config-data', (event, config) => {
            currentConfig = config;
            loadConfigToForm(config); // 调用新的加载函数
        });

        function showSaveNotification(msg = '保存成功', duration = 2500) {
            const el = document.getElementById('save-notification');
            el.textContent = msg;
            el.classList.add('show');
            setTimeout(() => {
                el.classList.remove('show');
            }, duration);
        }

        // 保存配置成功后调用
        if (window.require) {
            const { ipcRenderer } = require('electron');
            ipcRenderer.on('config-saved', () => {
                showSaveNotification('保存成功', 2500);
            });
        }
    </script>
</body>
</html> 